import 'dart:math';

import 'package:bot_toast/bot_toast.dart';
import 'package:exams_app/home_module/account_set_module/account_logic.dart';
import 'package:exams_app/net/Address.dart';
import 'package:exams_app/net/DioManager.dart';
import 'package:exams_app/net/persisten_storage.dart';
import 'package:exams_app/widget/hexcolor.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:convert';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

class UpdataAccountPage extends StatefulWidget {

  var nickname;
  UpdataAccountPage({this.nickname});

  @override
  _UpdataAccountPageState createState() => _UpdataAccountPageState();
}

class _UpdataAccountPageState extends State<UpdataAccountPage> {
  
  
  AccountLogic logic = Get.put(AccountLogic());
  
  /// 修改昵称
  // void UpdataDataWithNickName(var userAvatar) async {
  //   String result = await DioUtils(
  //           url: await PersistentStorage().getStorage('urlStr'))
  //       .postModel(
  //           '/api/v1/write/${await PersistentStorage().getStorage('partner_id')}',
  //           model: 'res.partner',
  //           params: {'form-field-char-name': userAvatar});
  //   Map json = jsonDecode(result);
  //
  //   BotToast.showText(text: json['message']);
  // }

  var tf1 = TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    tf1.text = widget.nickname;
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        resizeToAvoidBottomInset: false,
        backgroundColor: HexColor('#F9F9F9'),
        appBar: AppBar(
          title: Text('修改'),
          centerTitle: true,
          actions: [
            TextButton(
                onPressed: () {
                  if (tf1.text.isEmpty) {
                    BotToast.showText(text: '请输入昵称');
                    return;
                  }
                  // UpdataDataWithNickName(tf1.text);
                  
                  logic.requestDataWithapi_my_write('name', tf1.text);
                  Future.delayed(Duration(milliseconds: 400), () {
                    Get.back(result: 'refresh');
                  });
                },
                child: Text(
                  '保存',
                  style: TextStyle(color: Colors.white, fontSize: 16),
                ))
          ],
        ),
        body: ListView(
          children: [
            Container(
              padding: EdgeInsets.only(left: 15,right: 15),
              color: Colors.white,
              child: TextField(
                maxLength: 10,
                controller: tf1,
                decoration: InputDecoration(
                    border: InputBorder.none, hintText: '请输入昵称'),
              ),
            ),
          ],
        ));
  }
}

class UpdataAccountPasswordPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return UpdataAccountPasswordPageState();
  }
}

class UpdataAccountPasswordPageState extends State<UpdataAccountPasswordPage> {
  
  var tf1 = TextEditingController();
  var tf2 = TextEditingController();
  AccountLogic logic = Get.put(AccountLogic());

  // void requestData() async {
  //   // CustomProgressDialog progressDialog = CustomProgressDialog(context,blur: 0);
  //   //
  //   // progressDialog.show();
  //
  //   var prefs = await SharedPreferences.getInstance();
  //
  //   var params = {
  //     'access_token': prefs.getString('acctoken'),
  //     'uid': prefs.getInt('uid'),
  //     'partner_id': prefs.getInt('partner_id'),
  //     'model': 'res.users',
  //     'form-field-char-password': '${tf1.text}',
  //   };
  //
  //   String result = await DioUtils(url: prefs.getString('urlStr')).post(
  //       '/api/v1/write/${await PersistentStorage().getStorage('uid')}',
  //       data: params);
  //
  //
  //   Map json = jsonDecode(result);
  //   if (json['errmsg'] == 'ok') {
  //     // DioUtils().showToast('设置成功');
  //     // COToast.show('设置成功');
  //     BotToast.showText(text: '设置成功');
  //
  //     Navigator.pop(context);
  //     // progressDialog.dismiss();
  //   } else {
  //     // DioUtils().showToast(json['message']);
  //     // Toast.toast(context,msg: '${json['message']}');
  //     BotToast.showText(text: '${json['message']}');
  //
  //     // COToast.show(json['message']);
  //
  //   }
  // }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('修改密码'),
      ),
      body: Column(
        children: [
          // Container(
          //   child: TextField(
          //     controller: _phoneTf,
          //     decoration: InputDecoration(
          //       border: InputBorder.none,
          //       hintText: '请输入手机号码',
          //     ),
          //     cursorColor: Theme.of(context).primaryColor,
          //     keyboardType:TextInputType.name,
          //   ),
          //   padding: EdgeInsets.only(left: 20,right: 20,top: 20,bottom: 0)
          // ),
          // Divider(indent: 20,endIndent: 20,color: Colors.grey,),
          Container(
              child: TextField(
                // focusNode: _commentFocus,
                controller: tf1,
                decoration: InputDecoration(

                  border: InputBorder.none,
                  hintText: '请输入密码',
                ),
                inputFormatters: [
                  WhitelistingTextInputFormatter(RegExp(
                      "[a-zA-Z]|[0-9]")),
                  LengthLimitingTextInputFormatter(16)
                ],
                cursorColor: Theme.of(context).primaryColor,
                keyboardType: TextInputType.text,
                obscureText: true,
              ),
              padding:
                  EdgeInsets.only(left: 20, right: 20, top: 20, bottom: 0)),
          Divider(
            indent: 20,
            endIndent: 20,
            color: Colors.grey,
          ),

          Container(
              child: TextField(
                // focusNode: _commentFocus,
                controller: tf2,
                decoration: InputDecoration(
                  border: InputBorder.none,
                  hintText: '请再次确认密码',
                ),
                inputFormatters: [
                  WhitelistingTextInputFormatter(RegExp(
                      "[a-zA-Z]|[0-9]")),
                  LengthLimitingTextInputFormatter(16)
                ],
                cursorColor: Theme.of(context).primaryColor,
                keyboardType: TextInputType.text,
                obscureText: true,
              ),
              padding:
                  EdgeInsets.only(left: 20, right: 20, top: 20, bottom: 0)),
          Divider(
            indent: 20,
            endIndent: 20,
            color: Colors.grey,
          ),

          Container(
            height: 45,
            width: double.infinity,
            margin: EdgeInsets.all(20),
            child: RaisedButton(
              onPressed: () async {
                if (tf1.text.isEmpty) {

                  BotToast.showText(text: '请设置密码');

                  return;
                }

                if (tf1.text != tf2.text) {
                  // DioUtils().showToast('两次密码不一致',type: ToastGravity.CENTER);

                  // BotToast.toast(context,msg: '两次密码不一致');
                  BotToast.showText(text: '两次密码不一致');
                  return;
                }
                if(tf1.text.length<6){
                  BotToast.showText(text: '密码不得小于6位');

                  return;
                }
                //
                // requestData();
                logic.requestDataWithapi_my_write('password', tf1.text);

                Get.back();
                // requestData();
                // _commentFocus.unfocus();
              },
              color: Theme.of(context).primaryColor,
              child: Text(
                '设置密码',
                style: TextStyle(color: Colors.white),
              ),
            ),
          )
        ],
      ),
    );
  }
}
